package ext.tianma.workflow;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;

import wt.change2.WTChangeActivity2;
import wt.change2.WTChangeOrder2;
import wt.doc.WTDocument;
import wt.fc.ReferenceFactory;
import wt.fc.WTObject;
import wt.fc.WTReference;
import wt.httpgw.URLFactory;
import wt.inf.container.WTContainer;
import wt.org.WTUser;
import wt.part.WTPart;
import wt.pom.PersistenceException;
import wt.util.WTException;
import wt.vc.Iterated;
import wt.vc.VersionReference;
import wt.workflow.engine.WfActivity;
import wt.workflow.engine.WfProcess;
import wt.workflow.work.WorkItem;

public class WorkItemHelper {

	

	/**
	 * 获取已分配时间 dueDate
	 * 
	 * @param objRefself
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static String getDueDate(WorkItem workItem) throws WTException {
		String dateTime = "";
		String dateString ="";
		Date dueDate = null;
		if (workItem != null) {
			WfActivity activity = (WfActivity) workItem.getSource().getObject();
			dueDate = activity.getStartTime();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			if(dueDate!=null) {
				dateString = formatter.format(dueDate);
			}
			System.out.println("==>getDueDate方法-已分配时间:" + dateString);
		}
		return dateTime;
	}

	
	/**
	 * 获取流程业务对象链接url
	 * 
	 * @author he_zhang2
	 * @param objRefself
	 * @param obj
	 * @throws WTException
	 */
	public static String getUrl(WfProcess process) throws WTException {
		WTObject pbo = getPBO(process);
		String url = "";
		if (pbo != null) {
			if (pbo instanceof WTDocument) {
				WTDocument doc = (WTDocument) pbo;
				url = generateInfoPageURL(doc);
			} else if (pbo instanceof WTPart) {
				WTPart part = (WTPart) pbo;
				url = generateInfoPageURL(part);
			}
		}
		return url;
	}
	
	
	

	/**
	 * 获取流程业务对象链接url
	 * 
	 * @author he_zhang2
	 * @param objRefself
	 * @param obj
	 * @throws WTException
	 */
	public static String getPBOName(WfProcess process) throws WTException {
		WTObject pbo = getPBO(process);
		String name = "";
		if (pbo != null) {
			if (pbo instanceof WTDocument) {
				WTDocument doc = (WTDocument) pbo;
				name = doc.getName();
			} else if (pbo instanceof WTPart) {
				WTPart part = (WTPart) pbo;
				name = part.getName();
			}else if(pbo instanceof WTChangeOrder2){
				WTChangeOrder2 order = (WTChangeOrder2) pbo;
				name = order.getName();
			}
		}
		return name;
	}
	
	

	// 根据流程获取流程主对象
	public static WTObject getPBO(WfProcess wfprocess) throws WTException {
		WTObject wtobject = null;
		ReferenceFactory referencefactory = new ReferenceFactory();
		WTReference wtreference = wfprocess.getBusinessObjectReference(referencefactory);
		if (wtreference == null && wfprocess.isNested()) {
			wtobject = (WTObject) wfprocess.getContext().getValue("primaryBusinessObject");
		}
		if (wtreference != null && wtreference.getKey() != null) {
			wtobject = (WTObject) wtreference.getObject();
		}
		return wtobject;
	}

	/**
	 * 获取流程启动人工号 startUserId
	 * 
	 * @author xin_gao
	 * @param objRefself
	 * @param obj
	 * @return
	 * @throws WTException
	 */
	public static String getStartUserId(WfProcess process) throws WTException {
		String startUserName = process.getCreator().getName();
		WTUser startUser = (WTUser) process.getCreator().getPrincipal();
		String startUserId = getAlternateName(startUser, "alternateUserName1");
		System.out.println("==>getStartUserId方法-流程启动人:" + startUserName + "的工号:" + startUserId);
		String userId = startUser.getName();
		return userId;
	}
	
	
	/***
	 * 获取流程启动人工号 startUserId
	 * @param process
	 * @return
	 * @throws WTException 
	 */
	
	public static String getStartUserId2(WfProcess process) throws WTException {
		String startUserName = process.getCreator().getName();
		WTUser startUser = (WTUser) process.getCreator().getPrincipal();
		
		return null;
		
	}

	/**
	 * 获取任务处理人工号 assignee
	 * 
	 * @author he_zhang2
	 * @param objRefself
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static String getAssignee(WorkItem workItem) throws WTException {
		String assignee = null;
		if (workItem != null) {
			WTUser assigneeUser = (WTUser) workItem.getOwnership().getOwner().getObject();
			System.out.println("===assigneeUser==>>"+assigneeUser.getName());
			assignee = assigneeUser.getName();
//			assignee = getAlternateName(assigneeUser, "alternateUserName1");
//			System.out.println("==>getAssignee方法-任务处理人工号值:" + assignee);
		}
		return assignee;
	}

	// 获取WTDocument对象的详细页面URL
	public static String generateInfoPageURL(WTDocument doc) throws PersistenceException, WTException {
		String url = "";
		ReferenceFactory referenceFactory = new ReferenceFactory();
		URLFactory urlfactory = new URLFactory();
		String baseHref = urlfactory.getBaseHREF();
		if (doc != null && doc.getNumber() != null) {
			WTContainer container = doc.getContainer();
			String ufid = referenceFactory.getReferenceString(VersionReference.newVersionReference((Iterated) doc));
			String containerOid = "";
			if (container != null) {
				containerOid = referenceFactory.getReferenceString(container);
			}
			if ("".equals(containerOid)) {
				url = baseHref + "app/#ptc1/tcomp/infoPage?oid=" + ufid + "&u8=1";
			} else {
				url = baseHref + "app/#ptc1/tcomp/infoPage?ContainerOid=" + containerOid + "&oid=" + ufid + "&u8=1";
			}
			System.out.println("==>getUrl方法(doc对象)-流程业务对象链接url的url值:" + url);
		}
		return url;
	}

	// 获取WTPart对象的详细页面URL
	public static String generateInfoPageURL(WTPart part) throws PersistenceException, WTException {
		String url = "";
		ReferenceFactory referenceFactory = new ReferenceFactory();
		URLFactory urlfactory = new URLFactory();
		String baseHref = urlfactory.getBaseHREF();
		if (part != null && part.getNumber() != null) {
			WTContainer container = part.getContainer();
			String ufid = referenceFactory.getReferenceString(VersionReference.newVersionReference((Iterated) part));
			String containerOid = "";
			if (container != null) {
				containerOid = referenceFactory.getReferenceString(container);
			}
			if ("".equals(containerOid)) {
				url = baseHref + "app/#ptc1/tcomp/infoPage?oid=" + ufid + "&u8=1";
			} else {
				url = baseHref + "app/#ptc1/tcomp/infoPage?ContainerOid=" + containerOid + "&oid=" + ufid + "&u8=1";
			}
			System.out.println("==>getUrl方法(part对象)-流程业务对象链接url的url值:" + url);
		}
		return url;
	}

	// 获取工号
	public static String getAlternateName(WTUser paramWTUser, String paramString) throws WTException {
		String str = null;
		Enumeration localEnumeration = paramWTUser.getAttributes().getValues("uid");

		ArrayList localArrayList = Collections.list(localEnumeration);
		localArrayList.remove(paramWTUser.getName());
		if ((paramString.equals("alternateUserName1")) && (localArrayList.size() > 0))
			str = (String) localArrayList.get(0);
		else if ((paramString.equals("alternateUserName2")) && (localArrayList.size() > 1))
			str = (String) localArrayList.get(1);
		else if ((paramString.equals("alternateUserName3")) && (localArrayList.size() > 2))
			str = (String) localArrayList.get(2);
		else if ((paramString.equals("alternateUserName4")) && (localArrayList.size() > 3)) {
			str = (String) localArrayList.get(3);
		}
		return str;
	}
	
	
}
